package po

import (
	"GameGo/config"
	"golang.org/x/image/math/f64"
	"math"
	"math/rand"
)

type OrdinaryEnemy struct {
	X, Y       float64
	EnemyLevel int
	EnemyHP    int // 当前血量
	EnemyMaxHP int // 最大血量
	FireCD     int // 敌人子弹冷却
	MoveType   EnemyMoveType
	EnemyDir   f64.Vec2
	IsShotgun  bool
}

func NewOrdinaryEnemy(level int) *OrdinaryEnemy {
	temp := float64(config.ScreenXSize / 6)
	var lanes = []float64{temp, 2 * temp, 3 * temp, 4 * temp, 5 * temp, 6 * temp}
	x := lanes[rand.Intn(len(lanes))]
	enemy := &OrdinaryEnemy{
		EnemyLevel: level,
		X:          x - 65,
		Y:          -60,
		FireCD:     60,
		EnemyDir:   DirFromAngle(270),
		MoveType:   Ordinary,
		IsShotgun:  true,
	}
	enemy.EnemyMaxHP = enemy.EnemyLevel * config.EnemyHP
	enemy.EnemyHP = enemy.EnemyMaxHP
	return enemy

}

func (e *OrdinaryEnemy) EnemyMove() {

	e.Y -= 2 * e.EnemyDir[1]
	e.X += 2 * e.EnemyDir[0]

}

func (e *OrdinaryEnemy) EnemyShot(Bullets *[]Bullet, gx, gy float64) {
	if !e.IsShotgun {
		return
	}
	e.FireCD--
	if e.FireCD <= 0 {
		bullet := Bullet{
			BulletType:     EnemyBullet,
			BulletMoveType: Linear,
			BulletSpeed:    6.0,
			Damage:         e.EnemyLevel,
			Life:           180,
			Scale:          1,
		}

		dx := gx - e.X
		dy := gy - e.Y
		l := math.Hypot(dx, dy)
		bullet.Dir = f64.Vec2{dx / l, -dy / l}
		bullet.Position = f64.Vec2{e.X, e.Y + 30}
		*Bullets = append(*Bullets, bullet)
		e.FireCD = rand.Intn(90) + 120
	}

}

func (e *OrdinaryEnemy) EnemyOption(op EnemyOption, num int) (x, y float64, isDead bool, exp int) {
	switch op {
	case EnemyPosition:
		x, y = e.X, e.Y
	case HitEnemy:
		e.EnemyHP -= num
	case IsDead:
		if e.EnemyHP <= 0 {
			isDead = true
			return
		}
		isDead = false
	case OneKill:
		e.EnemyHP -= 9999
	case Exp:
		exp = e.EnemyMaxHP
		x, y = float64(e.EnemyHP), float64(e.EnemyMaxHP)
	case EnemyType:
		exp = int(e.MoveType)
	}
	return
}

func IsColliding(ax, ay, aw, ah, bx, by, bw, bh float64) bool {
	return ax < bx+bw && ax+aw > bx &&
		ay < by+bh && ay+ah > by
}
